#include "ZxPLModuleStat.h"

ZxPLModuleStat::ZxPLModuleStat(const QString& moduleName, QObject* parent)
    : QObject(parent)
{
    name = moduleName;
    alive = true;
    configured = false;
    pingInterval = DEFAULT_PING_INTERVAL;
    pingTimer = new QTimer(this);
    connect(pingTimer, SIGNAL(timeout()), this, SLOT(onPingTimerTimeout()));
    heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL;
    heartbeatTimer = new QTimer(this);
    connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(sendHeartbeat()));
}

ZxPLModuleStat::ZxPLModuleStat(const QString& moduleName, int heartbeatInterval, int pingInterval, QObject* parent)
    : QObject(parent)
{
    name = moduleName;
    alive = true;
    this->pingInterval = pingInterval;
    configured = false;
    pingTimer = new QTimer(this);
    connect(pingTimer, SIGNAL(timeout()), this, SLOT(onPingTimerTimeout()));
    this->heartbeatInterval = heartbeatInterval;
    heartbeatTimer = new QTimer(this);
    connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(sendHeartbeat()));
}

ZxPLModuleStat::~ZxPLModuleStat()
{
    if(pingTimer) {
        if(pingTimer->isActive())
            pingTimer->stop();
        delete pingTimer;
        pingTimer = NULL;
    }
    if(heartbeatTimer) {
        if(heartbeatTimer->isActive()) heartbeatTimer->stop();
        delete heartbeatTimer;
        heartbeatTimer = NULL;
    }
}

void ZxPLModuleStat::start()
{
    qDebug() << "starting module hearbeat";
    pingTimer->setInterval(getPingInterval() * 1000 * 60);
    pingTimer->start();
    alive = true;
    emit iAmAlive(getName());
    sendHeartbeat();
}

void ZxPLModuleStat::stop()
{
    pingTimer->stop();
}

void ZxPLModuleStat::kill()
{
    alive = false;
    stop();
    emit iAmDead(getName());
}

void ZxPLModuleStat::reset()
{
    pingTimer->stop();
    alive = true;
    pingTimer->setInterval(getPingInterval() * 1000 * 60);
    pingTimer->start();
}

void ZxPLModuleStat::onPingTimerTimeout()
{
    pingTimer->stop();
    if(alive) {
        alive = false; //dead state until the ping response
        pingTimer->setInterval(DEFAULT_WARNING_INTERVAL * 1000 * 60);
        pingTimer->start();
        emit iAmWarn(getName());
    }
    else {
        kill();
    }
}

void ZxPLModuleStat::sendHeartbeat()
{
    //emit heartbeat(settings.getName(), settings.getHeartbeatInterval());
    heartbeatTimer->stop();
    if(!configured) {
        heartbeatTimer->setInterval(CONFIGLESS_HEARTBEAT_INTERVAL*1000*60);
        QxPLMessage message(QxPLMessage::STATUS, "config", "basic");
        message.setSource(QxPLIdentifier("zigbus","module", getName()));
        message.setTarget(QxPLIdentifier::broadcast());
        message.setNamedValue("interval", QString::number(getHeartbeatInterval()));
        message.setNamedValue("version", "1.0");
        emit messageGenerated(message);
    }
    else {
        heartbeatTimer->setInterval(heartbeatInterval*1000*60);
        QxPLMessage message(QxPLMessage::STATUS, "hbeat", "basic");
        message.setSource(QxPLIdentifier("zigbus","module", getName()));
        message.setTarget(QxPLIdentifier::broadcast());
        message.setNamedValue("interval", QString::number(getHeartbeatInterval()));
        message.setNamedValue("version", "1.0");
        emit messageGenerated(message);
    }

    heartbeatTimer->start();
}

QString ZxPLModuleStat::toString() const
{
    QString rslt = "ZxPLModuleStat::toString()";
    return rslt;
}
